from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_admin.entity.do.ocr_config_do import OcrConfig
from module_admin.entity.vo.ocr_config_vo import OcrConfigModel, OcrConfigPageQueryModel
from utils.page_util import PageUtil


class OcrConfigDao:
    """
    OCR配置模块数据库操作层
    """

    @classmethod
    async def get_OcrConfig_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据ID获取OCR配置详细信息

        :param db: orm对象
        :param id: ID
        :return: OCR配置信息对象
        """
        OcrConfig_info = (
            (
                await db.execute(
                    select(OcrConfig)
                    .where(
                        OcrConfig.id == id
                    )
                )
            )
            .scalars()
            .first()
        )

        return OcrConfig_info

    @classmethod
    async def get_OcrConfig_detail_by_info(cls, db: AsyncSession, OcrConfig_model: OcrConfigModel):
        """
        根据OCR配置参数获取OCR配置信息

        :param db: orm对象
        :param OcrConfig_model: OCR配置参数对象
        :return: OCR配置信息对象
        """
        OcrConfig_info = (
            (
                await db.execute(
                    select(OcrConfig).where(
                        OcrConfig.id == OcrConfig_model.id if OcrConfig_model.id else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return OcrConfig_info

    @classmethod
    async def get_OcrConfig_list(cls, db: AsyncSession, query_object: OcrConfigPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取OCR配置列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: OCR配置列表信息对象
        """
        query = (
            select(OcrConfig)
            .where(
                OcrConfig.conf_key.like(f'%{query_object.conf_key}%') if query_object.conf_key else True,
                OcrConfig.conf_value == query_object.conf_value if query_object.conf_value else True,
                OcrConfig.conf_remark.like(f'%{query_object.conf_remark}%') if query_object.conf_remark else True,
            )
            .order_by(OcrConfig.id)
            .distinct()
        )
        OcrConfig_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return OcrConfig_list

    @classmethod
    async def add_OcrConfig_dao(cls, db: AsyncSession, OcrConfig_model: OcrConfigModel):
        """
        新增OCR配置数据库操作

        :param db: orm对象
        :param OcrConfig_model: OCR配置对象
        :return:
        """
        # 自增ID字段，新增时排除ID字段
        db_OcrConfig = OcrConfig(**OcrConfig_model.model_dump(exclude={'id'}, exclude_unset=True))
        db.add(db_OcrConfig)
        await db.flush()

        return db_OcrConfig

    @classmethod
    async def edit_OcrConfig_dao(cls, db: AsyncSession, OcrConfig_dict: dict):
        """
        编辑OCR配置数据库操作

        :param db: orm对象
        :param OcrConfig_dict: 需要更新的OCR配置字典
        :return:
        """
        await db.execute(update(OcrConfig).where(OcrConfig.id == OcrConfig_dict['id']), OcrConfig_dict)

    @classmethod
    async def delete_OcrConfig_dao(cls, db: AsyncSession, OcrConfig_model: OcrConfigModel):
        """
        删除OCR配置数据库操作

        :param db: orm对象
        :param OcrConfig_model: OCR配置对象
        :return:
        """
        await db.execute(delete(OcrConfig).where(OcrConfig.id.in_([OcrConfig_model.id])))

